package com.api.gbhg.huangwm.action;

import com.api.gbhg.huangwm.dao.RevenueBudgetDao;
import com.api.gbhg.liuwenbo.action.AmountVerification;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import com.customization.commons.LogTool;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author	Huangwm
 * @Date 2021-11-30
 * @Version 1.0
 * @description	TODO 一下二上提交后 用于记录核批的数据 用于在归档的时候跟二下核批的数据做对比
 */
public class ProjectApprovalData implements Action {
    private LogTool logTool = new LogTool("/log/gbhg/huangwm/action/ProjectApprovalData",false);
    private static RevenueBudgetDao budgetUtils = new RevenueBudgetDao();
    @Override
    public String execute(RequestInfo requestInfo) {
        logTool.writeLog("ProjectApprovalData一下二上提交后 记录核批的数据 requestid="+requestInfo.getRequestid());
        String submienodeid = requestInfo.getRequestManager().getSubmitNodeId();
        int nodeid = requestInfo.getRequestManager().getNodeid();
        int mainTableId = VersionFlowUtil.getFlowTableLableId(requestInfo.getWorkflowid()+"");//获取formid 例如：-83
        String mainTabName = AmountVerification.getMainTableName(mainTableId);//获取主表名
        Integer dw = AmountVerification.getwofkflowDw(mainTabName, requestInfo.getRequestid()+"");
        List<String> flowDetailTableNames = AmountVerification.getDetailTables(mainTableId);//获取明细表名称
        String flowTableName = VersionFlowUtil.getFlowTableName(requestInfo.getWorkflowid()+"");//获取表名
        String mainid = AmountVerification.getIdByTable(flowTableName, requestInfo.getRequestid()+""); //获取流程当前id

        String currentUserId = requestInfo.getRequestManager().getUserId()+"";
        String currentNodeid =  requestInfo.getRequestManager().getNodeid()+"";

        List<Map<String,String>> getSNDatas = new ArrayList<>();//上年结转
        List<Map<String,String>> getDNDatas = new ArrayList<>();//当年预算
        String ysnd = "";
        try {
            ysnd = budgetUtils.getFieldValue("ysnd", flowTableName, requestInfo.getRequestid()+"");
            if(ysnd.isEmpty()){
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                requestInfo.getRequestManager().setMessagecontent("ProjectApprovalData获取预算年度为空");
                return  FAILURE_AND_CONTINUE;
            }
        }catch (Exception e){
            requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
            requestInfo.getRequestManager().setMessagecontent("ProjectApprovalData获取预算年度异常 "+e.getMessage());
            return  FAILURE_AND_CONTINUE;
        }
        try {
            getSNDatas = budgetUtils.getSNDatas(flowDetailTableNames.get(1),mainid);//上年结转
            getDNDatas = budgetUtils.getDNDatas(flowDetailTableNames.get(0),mainid);//当年预算
            logTool.writeLog(" 上年结转 "+getSNDatas);
            logTool.writeLog(" 当年预算 "+getDNDatas);
        }catch (Exception e){
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
            requestInfo.getRequestManager().setMessagecontent("ProjectApprovalData获取上年结转和当年预算核批数据异常 "+e.getMessage());
            return  FAILURE_AND_CONTINUE;
        }

        try {
            String xmmc = budgetUtils.getFieldValue("xmmc", flowTableName, requestInfo.getRequestid()+"");
            String sqze = budgetUtils.getFieldValue("sqzey", flowTableName, requestInfo.getRequestid()+"");
            String hpze = budgetUtils.getFieldValue("hpze", flowTableName, requestInfo.getRequestid()+"");
            Map<String,String> mainData = new HashMap<>();
            mainData.put("lcid",requestInfo.getRequestid()+"");
            mainData.put("ysnd",ysnd);
            mainData.put("xmmc",xmmc);
            mainData.put("sqze",sqze);
            mainData.put("hpze",hpze);
            mainData.put("version", "start");
            Map<String, Object> result = budgetUtils.setPeojectApprovalRecords(mainData,getSNDatas,getDNDatas);
            if(!result.get("code").equals("1")){
                boolean f1 = (boolean) result.get("f1");
                boolean f2 = (boolean) result.get("f2");
                boolean f3 = (boolean) result.get("f3");
                String msg = "";
                if(!f1){
                    msg += "插入主表数据SQL执行异常;";
                }
                if(!f2){
                    msg += "插入上年结转核批数据SQL执行异常;";
                }
                if(!f3){
                    msg += "插入当年预算核批数据SQL执行异常;";
                }
                logTool.writeLog("ProjectApprovalData存档核批数据异常 msg="+msg);
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                requestInfo.getRequestManager().setMessagecontent("ProjectApprovalData存档核批数据异常>"+msg);
                return  FAILURE_AND_CONTINUE;
            }
        }  catch (Exception e) {
            e.printStackTrace();
            logTool.writeLog("存档核批数据异常 msg="+e.getMessage());
            requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
            requestInfo.getRequestManager().setMessagecontent("存档核批数据异常 "+e.getMessage());
            return  FAILURE_AND_CONTINUE;
        }
        logTool.writeLog("存档核批数据成功 requestid="+requestInfo.getRequestid());
        return SUCCESS;
    }
}
